<?php
session_start();

require ('../classes/UserManager.class.php');

if (isset($_SESSION['user'])) {
    header('Location: http://www.salesianichioggia.it/dashboard/index.php');
    die();
}


if (isset($_POST['submit'])) {
    $username = (isset($_POST['username'])) ? $_POST['username'] : '';
    $password = (isset($_POST['password'])) ? $_POST['password'] : '';

    $um = new UserManager();
    $user = $um->check_login($username, $password);

    if ($user == NULL) {
        $_SESSION['login_error'] = 1;
    } else {
        $_SESSION['user'] = serialize($user);
    }
    header('Location: http://www.salesianichioggia.it/dashboard/index.php');
    die();
}
?>

<!DOCTYPE html>
<html>
    <head>
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <link rel="shortcut icon" href="http://www.salesianichioggia.it/img/favicon.ico">
        <link type="text/css" rel="stylesheet" href="http://www.salesianichioggia.it/bootstrap/css/bootstrap.css" />
        <link type="text/css" rel="stylesheet" href="http://www.salesianichioggia.it/bootstrap/css/bootstrap-responsive.css" />
        <style>
            body {
                background: url('img/stressed_linen.png') repeat;
            }
            button#submit {
                margin-left: 2px;
            }
            legend {
                color: #FFF;
            }
            #login-box {
                margin-top: 200px;
            }
        </style>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"></script>
        <script src="http://crypto-js.googlecode.com/svn/tags/3.0.2/build/rollups/md5.js"></script>
        <script>
            $(document).ready(function(){
                $("input#username").focus();
                $("form#login").submit(function(){
                    var pwd = CryptoJS.MD5($("input#password").val());
                    $("input#password").val(pwd);
                    return true;
                });
            });
        </script>
    </head>
    <body>
        <div class="container">
            <div class="row">
                <div class="span4 offset4" id="login-box">
                    <legend>Dashboard :: salesianichioggia.it</legend>
                    <form class="form-horizontal" id="login" method="POST" action="http://www.salesianichioggia.it/dashboard/login.php">
                        <div class="control-group">
                            <input class="span4" type="text" name="username" id="username" placeholder="Username" />
                        </div>
                        <div class="control-group">
                            <input class="span4" type="password" name="password" id="password" placeholder="Password" />
                        </div>
                        <div class="control-group">
                            <button type="submit" class="btn btn-primary span4" name="submit" id="submit">Log in</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </body>
</html>